package com.glela.platform.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.glela.platform.mapper.PromotionDetailMapper;
import com.glela.platform.mapper.PromotionLogMapper;
import com.glela.platform.mapper.RedeemCodeLogMapper;
import com.glela.platform.mapper.RedeemCodeMapper;
import com.glela.platform.mapper.UserPromotionMapper;
import com.glela.platform.model.PromotionDetail;
import com.glela.platform.model.PromotionLog;
import com.glela.platform.model.RedeemCode;
import com.glela.platform.model.RedeemCodeLog;
import com.glela.platform.model.UserPromotion;
import com.glela.platform.service.RedeemCodeService;

/**
 * 兑换码
 * 
 * @author 卫耀华
 * 
 *
 */
@Service("redeemCodeService")
public class RedeemCodeServiceImpl implements RedeemCodeService {

	@Resource
	private RedeemCodeMapper redeemCodeMapper;
	@Resource
	private RedeemCodeLogMapper redeemCodeLogMapper;
	@Resource
	private PromotionDetailMapper promotionDetailMapper;
	@Resource
	private UserPromotionMapper userPromotionMapper;
	@Resource
	private PromotionLogMapper promotionLogMapper;

	/**
	 * 查询兑换码详情，根据兑换码查询
	 * 
	 * @param redeemCode
	 * @return
	 */
	@Override
	public RedeemCode selectRedeemCodeByRedeemCode(String redeemCode) {
		return redeemCodeMapper.selectRedeemCodeByRedeemCode(redeemCode);
	}

	@Override
	public RedeemCodeLog selectRedeemCodeLogByRedeemCodeIdAndUserId(int redeemCodeId, long userId) {
		return redeemCodeLogMapper.selectRedeemCodeLogByRedeemCodeIdAndUserId(redeemCodeId, userId);
	}

	@Override
	public List<PromotionDetail> selectPromotionDetailByPromotionId(int promotionId) {
		return promotionDetailMapper.selectPromotionDetailByPromotionId(promotionId);
	}

	@Override
	public UserPromotion selectUserPromotionByPromotionDetailIdAndUserId(int promotionDetailId, Long userId) {
		return userPromotionMapper.selectUserPromotionByPromotionDetailIdAndUserId(promotionDetailId, userId);
	}

	/**
	 * 添加优惠劵充换日志
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void insertPromotionLog(int promotionDeatailId, long userId, int drawChannel) {
		PromotionLog log = new PromotionLog();
		log.setPromotionDetailId(promotionDeatailId);
		log.setUserId(userId);
		log.setDrawChannel(drawChannel);
		promotionLogMapper.insertPromotionLog(log);
	}

	/**
	 * 修改兑换码已用次数
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateRedeemCodeUsedTimesById(int id, int usedTimes) {
		redeemCodeMapper.updateRedeemCodeUsedTimesById(id, usedTimes);
	}

	/**
	 * 添加兑换日志
	 * 
	 * @param codeLog
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void insertRedeemCodeLog(int redeemCodeId, long userId) {
		redeemCodeLogMapper.insertRedeemCodeLog(redeemCodeId, userId);
	}

}
